package cn.sz.dzy.shardingjdbcdemo.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;


@SuppressWarnings("WeakerAccess")
@Slf4j
public class FastJsonSerializer {

    public String serialize(Object object, SerializerFeature... features) {
        return JSON.toJSONString(object, features);
    }

    public String serializeWithClass(Object object) {
        return serialize(object, SerializerFeature.WriteClassName);
    }

    public Object deserialize(String string, Feature... features) {
        try {
            return JSON.parse(string, features);
        } catch (Exception e) {
            log.warn("deserialize object fail: {}", string);
            return null;
        }
    }

    public Object deserializeByClass(String string) {
        return deserialize(string, Feature.SupportAutoType);
    }

}
